package com.boy.controller;

import com.boy.commonutils.config.RabbitMQConfig;
import com.boy.service.AccountService;
import com.boy.service.weiXin.WxService;
import com.boy.tool.annotate.Arbitrarily;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.net.MalformedURLException;

/**
 * @author whd
 * @date 2024/08/16 12:00:07
 */
@RestController
@RequestMapping("/wx")
@Slf4j
public class WeiXinController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private WxService wxService;
    @Arbitrarily
    @GetMapping("/put")
    public void setMessage(@RequestParam String to) throws WxErrorException {
        //accountService.sendAll();
        accountService.send(to);
    }

    @Arbitrarily
    @GetMapping("send")
    public void sendReminder(@RequestParam String message) {
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setExpiration(String.valueOf(1000*60)); // 设置过期时间（毫秒）
        Message msg = new Message(message.getBytes(), messageProperties);
        rabbitTemplate.send(RabbitMQConfig.EXCHANGE_NAME,  RabbitMQConfig.ROUTING_KEY, msg);
        log.info("发送消息"+message);
    }

    @Arbitrarily
    @GetMapping("menu")
    public void setMenu() throws MalformedURLException, WxErrorException {
        wxService.setMenu();
    }
}
